<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/src/css/layui.css"  media="all">
    <script src="js/JQ.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <!--<div class="layui-btn-container">-->
        <!--<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>-->
        <!--<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>-->
        <!--<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>-->
    <!--</div>-->
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="yes">
    <button onclick="yes({{ d.id_register_requests }})" class="layui-btn">同意</button>
</script>
<script type="text/html" id="no">
    <button onclick="no({{ d.id_register_requests }})" style="background-color: red" class="layui-btn">拒绝</button>
</script>

<script>
    function yes(value) {
        $.post(
            "/register_requests/add_to_user",
            {
                "id_register_requests":value
            },
            function (data,states) {
                window.location.reload();
            }
            );
    }
    function no(value) {
        $.post(
            "/register_requests/delete_register_requests",
            {
                "id_register_requests":value
            },
            function (data,states) {
                window.location.reload();
            }
            );
    }
</script>

<script src="layui/src/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;

        var count = 100;
        page();
        function page(){
            $.ajax({
                url: '/register_requests/get_register_requests_count',
                dataType: 'json',
                type: 'get',
                async:false,
                success: function(data){
                    console.log(data.count)
                    count = data.count;
                }
            });
        }

        table.render({
            elem: '#test'
            ,url:'/register_requests/get_register_requests_data' //改地址是访问地址
            // ,data: {
            //     page:1, //当前页数
            //     limit:2 //每页显示数量
            // }
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                // {type: 'checkbox', fixed: 'left'},
                {field:'id_register_requests', width:100, title: 'ID', sort: true}
                ,{field:'name', width:100, title: '姓名'}
                ,{field:'username', width:120, title: '学号'}
                ,{field:'phone', width:120, title: '电话'}
                ,{field:'grade', width:100, title: '年级'}
                ,{field:'role', width:100, title: '角色'}
                ,{field:'password', width:100, title: '密码'}
                ,{field:'id_register_requests',toolbar: '#yes', width:100}
                ,{field:'id_register_requests',toolbar: '#no', width:100}
            ]]
            ,page: true
            ,limit:10
            ,response:{
                statusName: 'status' //规定数据状态的字段名称，默认：code
                ,dataName: 'rows' //规定数据列表的字段名称，默认：data
            }
            ,parseData: function(item){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "status": 0,   //解析接口状态
                    "message":"成功",  //解析提示文本
                    "count": count,       //解析数据长度
                    "rows": item.data  //解析数据列表
                };
            }
        });


        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
</html>